import { Application } from "egg";
import { ObjectId } from "mongoose";
import * as AutoIncrementFactory from "mongoose-sequence";
export interface CompanyInfoProps {
  company_adr: string; //【公司地址】
  company_funds: number; //【注册资本/单位（万）】
  company_legal: string; //【法定代表人】
  company_credit_code: string; //【统一社会信用代码】
  company_org_code: string; //【组织机构代码】
  company_taxpayer_code: string; //【纳税人识别号】
  company_type: string; //【企业类型】
  company_register: string; //【登记机关】
  company_date: string; //【成立日期】
  company_id: ObjectId; //【关联公司ID】
}
function initCompanyInfoModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const CompanyInfoScheme = new Schema<CompanyInfoProps>(
    {
      company_adr: { type: String },
      company_funds: { type: String, required: true },
      company_legal: { type: String, required: true },
      company_credit_code: { type: String, unique: true, required: true },
      company_org_code: { type: String, unique: true, required: true },
      company_taxpayer_code: { type: String },
      company_type: { type: String },
      company_register: { type: String },
      company_date: { type: String, required: true },
      company_id: {
        type: Schema.Types.ObjectId,
        ref: "Company",
        unique: true,
        required: true,
      },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  CompanyInfoScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "Company_info_counter",
  });
  return app.mongoose.model<CompanyInfoProps>("CompanyInfo", CompanyInfoScheme);
}

export default initCompanyInfoModel;
